﻿using System;
using System.Collections.Generic;
using SlimDX;

namespace Editor.Command
{
    internal class RotateEntitiesCommand : ICommand
    {
        private List<Entity> m_Entities;
        private List<Matrix> m_InitialTransforms = new List<Matrix>();
        private Quaternion m_Rotation;

        public String Name
        {
            get
            {
                if (m_Entities.Count > 0)
                    return "Rotate Entities";
                return "Rotate Entity";
            }
        }

        public RotateEntitiesCommand(List<Entity> entities, Quaternion rotation)
        {
            m_Entities = entities;
            m_Rotation = rotation;

            foreach (Entity entity in m_Entities)
            {
                m_InitialTransforms.Add(entity.LocalTransform);
            }
        }

        public void Merge(ICommand change)
        {
            RotateEntitiesCommand newRot = change as RotateEntitiesCommand;
            m_Rotation *= newRot.m_Rotation;
        }

        public void Do()
        {
            foreach (Entity entity in m_Entities)
            {
                entity.Rotate(m_Rotation);
            }
        }

        public void Undo()
        {
            for (int i = 0; i < m_Entities.Count; i++)
            {
                Entity entity = m_Entities[i];
                Matrix transform = m_InitialTransforms[i];
                
                entity.LocalTransform = transform;
            }
        }
    }
}
